//
//  ViewController.m
//  test
//
//  Created by 张征鸿 on 16/11/1.
//  Copyright © 2016年 张征鸿. All rights reserved.
//

#import "ViewController.h"
#import "RoomGift.h"
#import "TableViewCell.h"
#import <UITableView+FDTemplateLayoutCell.h>
#import "XRAFNetworkTool.h"
#import "Medals.h"
#import <WebKit/WebKit.h>
#import "ViewController+Delegate.h"
static NSString * const kTableViewCell = @"TableViewCell";
@interface ViewController ()<roomGiftDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) RoomGift *gift;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) TableViewCell *tCell;

@property (nonatomic, strong) WKWebView *wkWebView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self.view addSubview:self.gift];
    
//    [self.view addSubview:self.tableView];
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-50, 200, 50, 50)];
//    view.backgroundColor = [UIColor orangeColor];
//    [self.view addSubview:view];
//    
//    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(-50, 300, 50, 50)];
//    view2.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:view2];
//    
//    NSLog(@"%f, %f", view.frame.origin.x, view.layer.position.x);
//    [self animation:view];
//    [self systemAnimation:view2];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self animation:view];
    });
    NSLog(@"%d",   [[NSUserDefaults standardUserDefaults] boolForKey:@"1"]);
}

- (void)animation:(UIView *)view
{
//    CABasicAnimation *animation = [[CABasicAnimation alloc] init];
//    animation.keyPath = @"transform.translation.x";
//    animation.toValue = @(view.frame.size.width + 250);
//    animation.duration = 3;
//    animation.removedOnCompletion = NO;
//    animation.fillMode = kCAFillModeForwards;
//    [view.layer addAnimation:animation forKey:@"p"];
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGPoint p1 = {25, 25};
    CGPoint p2 = {[UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2};
    CGPoint p3 = {25, [UIScreen mainScreen].bounds.size.height - 25};
    CGPoint p4 = {[UIScreen mainScreen].bounds.size.width - 25, [UIScreen mainScreen].bounds.size.height - 25};
    NSValue *value = [NSValue valueWithCGPoint:p1];
    NSValue *value2 = [NSValue valueWithCGPoint:p2];
    NSValue *value3 = [NSValue valueWithCGPoint:p3];
    NSValue *value4 = [NSValue valueWithCGPoint:p4];
    keyAnimation.duration = 10;
    keyAnimation.fillMode = kCAFillModeForwards;            /**< 需同时设定，动画会保持最后状态 与下一步 */
    keyAnimation.removedOnCompletion = false;
    keyAnimation.values = @[value, value2, value3, value4];
    [view.layer addAnimation:keyAnimation forKey:@"key"];
}

- (void)systemAnimation:(UIView *)view
{
    [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        view.frame = CGRectMake(250, 300, 50, 50);
    } completion:^(BOOL finished) {
        
    }];
}

- (RoomGift *)gift
{
    if (!_gift) {
        _gift = [[RoomGift alloc] initWithFrame:CGRectMake(16, 300, [UIScreen mainScreen].bounds.size.width - 32, 100)];
        _gift.layer.masksToBounds = YES;
        _gift.layer.cornerRadius = 10;
        _gift.delegate = self;
        _gift.backgroundColor = [UIColor orangeColor];
    }
    return _gift;
}

- (void)callBackViewHeight:(CGFloat)height
{
    self.gift.frame = CGRectMake(16, 300, [UIScreen mainScreen].bounds.size.width - 32, height);
}


#pragma mark - tableView

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc ]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        [_tableView registerNib:[UINib nibWithNibName:kTableViewCell bundle:nil] forCellReuseIdentifier:kTableViewCell];
    }
    return _tableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return [tableView fd_heightForCellWithIdentifier:kTableViewCell cacheByIndexPath:indexPath configuration:^(TableViewCell *cell) {
//        
//    }];
    self.tCell.titleLabel.preferredMaxLayoutWidth = 100;
    return [self.tCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCell];
    self.tCell = cell;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
